package entities;

import java.io.Serializable;

import javax.persistence.*;

import ejb.dto.PeriodDTO;

import java.util.Date;
import java.util.List;


/**
 * The persistent class for the periods database table.
 * 
 */
@Entity
@Table(name="periods", uniqueConstraints={
			@UniqueConstraint(columnNames={"FROM_DATE", "TO_DATE"}),
			@UniqueConstraint(columnNames={"NAME"})
})
@NamedQueries({
	@NamedQuery(name="Period.findAll", query="SELECT p FROM Period p ORDER BY p.fromDate ASC"),
	@NamedQuery(name="Period.findBySingleDate", query="SELECT p FROM Period p WHERE p.fromDate<=:d AND :d<=p.toDate")
})
public class Period implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;

	@Lob
	private String description;

	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
	@Column(name="FROM_DATE")
	private Date fromDate;

	private String name;

	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
	@Column(name="TO_DATE")
	private Date toDate;

	/**
	 * bi-directional many-to-one association to Package
	 */
	@OneToMany(mappedBy="period")
	private List<Package> packages;

	
	//CONSTRUCTORS
	public Period() {
	}

	public Period(PeriodDTO period){
		this.id = period.getId();
		this.name = period.getName();
		this.description = period.getDescription();
		this.fromDate = period.getFromDate();
		this.toDate = period.getToDate();
	}
	
	//GETTER AND SETTER
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getFromDate() {
		return this.fromDate;
	}

	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getToDate() {
		return this.toDate;
	}

	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}

	public List<Package> getPackages() {
		return this.packages;
	}

	public void setPackages(List<Package> packages) {
		this.packages = packages;
	}

	//METHODS
	
	/**
	 * add Package to list
	 * @param pack
	 * @return list of Package
	 */
	public Package addPackage(Package pack) {
		getPackages().add(pack);
		pack.setPeriod(this);

		return pack;
	}

	/**
	 * remove Package from list
	 * @param pack
	 * @return list of Package
	 */
	public Package removePackage(Package pack) {
		getPackages().remove(pack);
		pack.setPeriod(null);

		return pack;
	}
	

}